package com.rock.code.system.roleModule.controller;

import com.alibaba.fastjson.JSONObject;
import com.github.pagehelper.PageInfo;
import com.rock.code.base.controller.BaseController;
import com.rock.code.base.domain.AjaxJson;
import com.rock.code.base.domain.Constants;
import com.rock.code.common.excel.ExportExcel;
import com.rock.code.common.exceptions.SystemException;
import com.rock.code.common.pager.Page;
import com.rock.code.system.roleModule.domain.TbSysRoleModule;
import com.rock.code.system.roleModule.service.TbSysRoleModuleService;
import com.rock.code.system.user.domain.TbSysLoginmsg;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;

import javax.annotation.Resource;
import java.util.Date;
import java.util.HashMap;
import java.util.List;
import java.util.Map;



/**
* 权限管理
*
* @author wangwenbo
* @date 2018/01/15
*/
@Controller
@RequestMapping("/tbSysRoleModule")
public class TbSysRoleModuleController extends BaseController {

    /**
    * The tbSysRoleModuleServiceImpl
    */
    @Resource
    private TbSysRoleModuleService tbSysRoleModuleServiceImpl;

    /**
    * Dispatch.
    *
    * @return the string
    * @throws Exception the exception
    * @author wangwenbo
    * @date 2018/01/15
    */
    @RequestMapping("")
    public String dispatch() throws Exception {
        return "system/roleModule/tbSysRoleModuleList";
    }

    /**
    * queryPageList
    *
    * @return the page info
    * @throws Exception the exception
    * @author wangwenbo
    * @date 2018/01/15
    */
    @ResponseBody
    @RequestMapping(value = "/queryPageList", method = RequestMethod.POST)
    public PageInfo<TbSysRoleModule> queryPageList() throws Exception {
        Map<String, Object> param = this.getRequestMapSingle();
        Page page = new Page(request);
        PageInfo pageInfo = null;
        try {
            param.put("isDelete", Constants.DELETE_N);
            pageInfo = tbSysRoleModuleServiceImpl.queryPageList(page, param);
        } catch (Exception e) {
            e.printStackTrace();
            throw new SystemException(e);
        }
        return pageInfo;
    }

    /**
     * querysList
     *
     * @return the list
     * @throws Exception the exception
     * @author wangwenbo
     * @date 2018/02/11
     */
    @ResponseBody
    @RequestMapping(value = "/queryList", method = RequestMethod.POST)
    public List<TbSysRoleModule> queryListByMap() throws Exception {
        Map<String, Object> param = this.getRequestMapSingle();
        List<TbSysRoleModule> list = null;
        try {
            param.put("isDelete", Constants.DELETE_N);
            list = tbSysRoleModuleServiceImpl.queryListByParaMap(param);
        } catch (Exception e) {
            e.printStackTrace();
            throw new SystemException(e);
        }
        return list;
    }

    /**
     * queryPageList
     *
     * @return the page info
     * @throws Exception the exception
     * @author wangwenbo
     * @date 2018/01/15
     */
    @ResponseBody
    @RequestMapping(value = "/queryTablePageList", method = RequestMethod.POST)
    public PageInfo<TbSysRoleModule> queryTablePageList() throws Exception {
        Map<String, Object> param = this.getRequestMapSingle();
        Page page = new Page(request);
        PageInfo pageInfo = null;
        try {
            param.put("isDelete", Constants.DELETE_N);
            pageInfo = tbSysRoleModuleServiceImpl.queryTablePageList(page, param);
        } catch (Exception e) {
            e.printStackTrace();
            throw new SystemException(e);
        }
        return pageInfo;
    }

    /**
    * querySingle
    *
    * @return TbSysRoleModule
    * @throws Exception the exception
    * @author wangwenbo
    * @date 2018/01/15
    */
    @ResponseBody
    @RequestMapping(value = "/querySingle", method = RequestMethod.POST)
    public TbSysRoleModule querySingle() throws Exception {
        Map<String, Object> param = this.getRequestMapSingle();
        TbSysRoleModule tbSysRoleModule = null;
        try {
            tbSysRoleModule = tbSysRoleModuleServiceImpl.queryByParaMap(param);
        } catch (Exception e) {
            e.printStackTrace();
            throw new SystemException(e);
        }
        return tbSysRoleModule;
    }

    /**
    * Save.
    *
    * @return the ajax json
    * @throws Exception the exception
    * @author wangwenbo
    * @date 2018/01/15
    */
    @ResponseBody
    @RequestMapping(value = "/save", method = RequestMethod.POST)
    public AjaxJson save() throws Exception {

        try {
            TbSysLoginmsg sessionUser = getSessionUser();
            Map<String, Object> param = this.getRequestMapSingle();

            Map<String, Object> roleCodeParam = new HashMap<String, Object>();
            Map<String, String[]> moduleCodeParam = new HashMap<String, String[]>();

            roleCodeParam.put("roleCode", param.get("roleCode"));
            String moduleCodeStr = (String) param.get("moduleCode");
            moduleCodeParam.put("moduleCode", moduleCodeStr.split(","));

            // 删除原权限
            tbSysRoleModuleServiceImpl.deleteByParam(roleCodeParam);
            // 设置新权限
            for (String moduleCode : moduleCodeParam.get("moduleCode")) {
                if(isNotEmpty(moduleCode)) {
                    TbSysRoleModule tbSysRoleModule = new TbSysRoleModule();

                    tbSysRoleModule.setRoleCode((String) param.get("roleCode"));
                    tbSysRoleModule.setModuleCode(moduleCode);
                    tbSysRoleModule.setIsDelete(Constants.DELETE_N);
                    tbSysRoleModule.setCreater(sessionUser.getLoginCode());
                    tbSysRoleModule.setCreateDate(new Date());
                    tbSysRoleModule.setModifier(sessionUser.getLoginCode());
                    tbSysRoleModule.setModifieDate(new Date());

                    tbSysRoleModule.setRoleId((String) param.get("roleCode"));
                    tbSysRoleModule.setModuleId(moduleCode);

                    tbSysRoleModuleServiceImpl.insert(tbSysRoleModule);
                }
            }
            return ajaxJson;
        } catch (Exception e) {
            e.printStackTrace();
            throw new SystemException(e);
        }

    }

    /**
    * Delete.
    *
    * @param ids the ids
    * @return the ajax json
    * @throws Exception the exception
    * @author wangwenbo
    * @date 2018/01/15
    */
    @ResponseBody
    @RequestMapping(value = "/delete", method = RequestMethod.POST)
    public AjaxJson delete(@RequestParam String ids) throws Exception {
        try {
            String[] roleModuleIds = ids.split(",");
            TbSysLoginmsg sessionUser = getSessionUser();
            for (String roleModuleId : roleModuleIds) {
                TbSysRoleModule tbSysRoleModule = new TbSysRoleModule();
                tbSysRoleModule.setRoleModuleId(roleModuleId);
                tbSysRoleModule.setIsDelete(Constants.DELETE_Y);
                tbSysRoleModule.setModifier(sessionUser.getLoginCode());
                tbSysRoleModule.setModifieDate(new Date());
                tbSysRoleModuleServiceImpl.update(tbSysRoleModule);
            }
            return ajaxJson;
        } catch (Exception e) {
            e.printStackTrace();
            throw new SystemException(e);
        }
    }

    /**
    * Exp excel.
    *
    * @return the ajax json
    * @throws Exception the exception
    * @author wangwenbo
    * @date 2018/01/15
    */
    @ResponseBody
    @RequestMapping(value = "/expExcel", method = RequestMethod.POST)
    public AjaxJson expExcel() throws Exception {
        try {
            Map<String, Object> param = this.getRequestMapSingle();
            String[] titles = (String[]) param.get("titles[]");
            String[] fields = (String[]) param.get("fields[]");
            Page page = new Page((String) param.get("sidx"), (String) param.get("sord"));
            param.put("isDelete", Constants.DELETE_N);
            ExportExcel ee = new ExportExcel();
            List rows = tbSysRoleModuleServiceImpl.queryTableListByOrder(page, param);
            String path = ee.exportExcel(request, response, 40000, titles, fields, rows, TbSysRoleModule.class.getName());
            ajaxJson.setResult(path);
        } catch (Exception e) {
            e.printStackTrace();
            throw new SystemException(e);
        }
        return ajaxJson;
    }

    /**
    * Check input field.
    *
    * @param inputName the input name
    * @param inputValue the input value
    * @return the json object
    * @throws Exception the exception
    * @author wangwenbo
    * @date 2018/01/15
    */
    @ResponseBody
    @RequestMapping(value = "/checkInputField", method = RequestMethod.POST)
    public JSONObject checkInputField(String inputName, String inputValue) throws Exception {
        try {
            Map<String, Object> param = new HashMap<String, Object>();
            param.put(inputName, inputValue);
            param.put("isDelete", Constants.DELETE_N);
            List rows = tbSysRoleModuleServiceImpl.queryListByParaMap(param);
            if (rows != null && rows.size() > 0) {
                json.put("valid", false);
            } else {
                json.put("valid", true);
            }
        } catch (Exception e) {
            e.printStackTrace();
            throw new SystemException(e);
        }
        return json;
    }
}
